﻿using BankInterface;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;

namespace MEFDemo
{
    class Program
    {
        //其中AllowRecomposition=true参数就表示运行在有新的部件被装配成功后进行部件集的重组.


        static void Main(string[] args)
        {
            PartCompose pro = new PartCompose();
            pro.Compose();
            foreach (var c in pro.cards)
            {
                if (c.Metadata.CardType ==CardType.BOC)
                {
                    Console.WriteLine("Here is a card of Bank Of China ");
                    Console.WriteLine(c.Value.GetCountInfo());
                }
                if (c.Metadata.CardType == CardType.ZH)
                {
                    Console.WriteLine("Here is a card of Zhao ");
                    Console.WriteLine(c.Value.GetCountInfo());
                }
            }
            Console.Read();
        }


    }
    public class  PartCompose
    {
        [ImportMany(AllowRecomposition = true)]
        public IEnumerable<Lazy<ICard, IMetaData>> cards { get; set; }

        public void Compose()
        {
            var catalog = new DirectoryCatalog("Cards");//得到目录下所以dll
            var container = new CompositionContainer(catalog);
            container.ComposeParts(this);//在指定的组合容器中组合
        }

    }
}
